package com.me.spider;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

@Component
public class WebMegicTimer implements PageProcessor, ApplicationContextAware {

    @Autowired
    private static ApplicationContext applicationContext;

    // 爬url
    List<String> linkList;
    // 爬内容
    List<String> contentList;
    // 爬日期
    List<String> dateList;

    private Site site = Site.me().setDomain("index.html");

    public static void main(String args[]) {
        Spider.create(new WebMegicTimer()).addUrl("http://dangjian.people.com.cn/GB/394443/index.html")
            .addPipeline(new ConsolePipeline()).run();
    }

    @Override
    public void process(Page page) {
        List<String> links = page.getHtml().links().regex("http://dangjian.people.com.cn/GB/394443/index.html").all();
        page.addTargetRequests(links);
        // 爬标题
        String title = page.getHtml().xpath("//div[@class='fl']/h2/text()").toString();
        // page.putField("content", page.getHtml().$("div.content").toString());
        // 爬url
        linkList = page.getHtml().xpath("//div[@class='fl']/ul/li/a/@href").all();
        // 爬内容
        contentList = page.getHtml().xpath("//div[@class='fl']/ul/li/a/text()").all();
        // 爬日期
        dateList = page.getHtml().xpath("//div[@class='fl']/ul/li/i/text()").all();
        insetDate(linkList, contentList, dateList);

    }

    private void insetDate(List<String> linkList, List<String> contentList, List<String> dateList) {

        // this.webMegicService = (WebMegicService) WebMegicTimer.getBean("webMegicService");
        // this.userService= ApplicationContextProvider.getBean(UserService .class);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String dateFormat = sdf.format(date);
        System.out.println(dateFormat);

        // int isCountInfo = webMegicService.isCountInfo();
        // System.out.println(isCountInfo);
        for (int i = 0; i < linkList.size(); i++) {
            String cd = dateList.get(i).substring(1, dateList.get(i).length() - 1);
            // // 判断是否表中有值
            // if (isCountInfo != 0) {
            // if (cd.equals(dateFormat)) {
            // // 插表
            // webMegicService.insertWebMegicInfo(linkList.get(i),
            // dateList.get(i).substring(1, dateList.get(i).length() - 1), contentList.get(i), "90001");
            // }
            // } else {
            // // 循环插表
            // webMegicService.insertWebMegicInfo(linkList.get(i),
            // dateList.get(i).substring(1, dateList.get(i).length() - 1), contentList.get(i), "90001");
            // }

            printLn(linkList.get(i), dateList.get(i).substring(1, dateList.get(i).length() - 1), contentList.get(i),
                    "90001");
        }
    }

    public void printLn(Object... args) {
        Arrays.stream(args).forEach(System.out::println);
    }

    @Override
    public Site getSite() {
        // site.setCharset("UTF-8");
        return site;
    }

    @Scheduled(cron = "0 0 1 * * ? ")
    public void scheduled() {
        Spider.create(new WebMegicTimer()).addUrl("http://dangjian.people.com.cn/GB/394443/index.html")
                .addPipeline(new ConsolePipeline()).run();
    }

    /*
     * @Scheduled(cron="0/5 * * * * ? ") public void scheduled(){ Spider.create(new
     * WebMegicTimer()).addUrl("http://dangjian.people.com.cn/GB/394443/index.html") .addPipeline(new
     * ConsolePipeline()).run(); }
     */

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        WebMegicTimer.applicationContext = applicationContext;

    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

}
